
import axios from "axios";
import { ElMessage } from "element-plus";
import router from "@/router";
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.interceptors.request.use(function(config){
    return config
},function(error){
    ElMessage({
        message:'服务异常,请联系管理员！',
        icon:'close',
    });
    return Promise.reject(error)
})

axios.interceptors.response.use(function(response){
    return response
},function(error){
    if(error.code === 'ERR_BAD_RESPONSE'){
        ElMessage({
            message:'错误：ERR_BAD_RESPONSE,请联系管理员！',
            icon:'close',
        });
    }else if(error.code === "ERR_BAD_REQUEST" && error.response.status === 401){
        router.push('/index.php/upload/uploadAvatar');
        ElMessage({
            message:error.response.data.message,
            icon:'close',
        });
    }else{
        ElMessage({
            message:'未知错误,请联系管理员！',
            icon:'close',
        });
    }
    return Promise.reject(error)
})

///////////////////////////////////////////////////////////
// 用户
export const addUser = function(data:any){
    return axios.post('/index.php/User/user',data);
}

export const getUser = function(id:string|number=''){
    return axios.get(`/index.php/User/user/${id}`)
}

export const deleteUser = function(id:string|number){
    return axios.delete(`/index.php/User/user/${id}`)
}

export const putUser = function(id:string|number,data:any){
    return axios.put(`/index.php/User/user/${id}`,data);
}

///////////////////////////////////////////////////////////
// 角色
export const addRole = function(data:any){
    return axios.post('/index.php/Role/role',data);
}

export const getRole = function(id:string|number = ''){
    return axios.get(`/index.php/Role/role/${id}`)
}

export const deleteRole = function(id:string|number){
    return axios.delete(`/index.php/Role/role/${id}`)
}

export const putRole = function(id:string|number,data:any){
    return axios.put(`/index.php/Role/role/${id}`,data)
}

///////////////////////////////////////////////////////////
// 资源
export const addResources = function(data:any){
    return axios.post('/index.php/Resources/resources',data);
}

export const getResources = function(id:string|number = ''){
    return axios.get(`/index.php/Resources/resources/${id}`)
}

export const deleteResources = function(id:string|number){
    return axios.delete(`/index.php/Resources/resources/${id}`)
}

export const putResources = function(id:string|number,data:any){
    return axios.put(`/index.php/Resources/resources/${id}`,data)
}


////////////////////////////////////////////////////
// 登录

export const login = function(data:any){
    return axios.post('/index.php/Login/login',data);
}

export const logout = function(){
    return axios.get('/index.php/Login/logout');
}

export const getAuth = function(){
    return axios.get('/index.php/Login/getAuth')
}

/////////////////////////////////////////////////////////
// 角色权限管理  RoleResources
export const getRoleResources = function(id:number|string){
    return axios.get(`/index.php/RoleResources/RoleResources/${id}`);
}

export const addRoleResources = function(data:any){
    return axios.post('/index.php/RoleResources/RoleResources',data)
}

export const deleteRoleResources = function(id:string|number){
    return axios.delete(`/index.php/RoleResources/RoleResources/${id}`)
}

export const putRoleResources = function(id:string|number,data:any){
    return axios.put(`/index.php/RoleResources/RoleResources/${id}`,data)
}


//////////////////////////////////////////////////////
// 用户权限管理
export const getUserRole = function(id:string|number){
    return axios.get(`/index.php/UserRole/UserRole/${id}`)
}

export const addUserRole = function(data:any){
    return axios.post(`/index.php/UserRole/UserRole`,data);
}

export const deleteUserRole = function(id:string|number){
    return axios.delete(`/index.php/UserRole/UserRole/${id}`)
}

export const putUserRole = function(id:string|number,data:any){
    return axios.put(`/index.php/UserRole/UserRole/${id}`,data)
}

/////////////////////////////////////////////////////
// 进出站数据

export const getStationMange = function(id:string|number){
    return axios.get(`/index.php/StationMange/StationMange/${id}`);
}

export const deleteStationMange = function(id:string|number){
    return axios.delete(`/index.php/StationMange/StationMange/${id}`);
}

export const searchStationMange = function(page = 1,number = 100,data:any){
    return axios.post(`/index.php/StationMange/StationMangeSearch/${page}/${number}`,data);
}